﻿Imports TextBoxPrj
Public Class SyntaxRule
    Private mConfig As ClsCodeConfig
    Private mSyntaxDic As New Dictionary(Of String, SyntaxKey)
    Sub New(ByVal _Config As ClsCodeConfig)
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。
        mConfig = _Config
    End Sub
    Private Sub SyntaxRule_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Text = GetTextInLanguageDoc("SyntaxRule", TextMode.Text)
        Me.cmdAddSyntaxRuler.Text = GetTextInLanguageDoc("AddOrModify", TextMode.Text)
        Me.cmdDelSyntaxRuler.Text = GetTextInLanguageDoc("DeleteFromList", TextMode.Text)
        Me.lblLayoutName.Text = GetTextInLanguageDoc("LayoutName", TextMode.Text)
        Me.lblLayoutLV.Text = GetTextInLanguageDoc("LayoutLV", TextMode.Text)

        Me.lblStartRegex.Text = GetTextInLanguageDoc("StartRegex", TextMode.Text)
        Me.lblEndRegex.Text = GetTextInLanguageDoc("EndRegex", TextMode.Text)

        Me.lstSyntaxKeyList.Columns(0).Text = GetTextInLanguageDoc("LayoutName", TextMode.Text)
        Me.lstSyntaxKeyList.Columns(1).Text = GetTextInLanguageDoc("LayoutLV", TextMode.Text)
        Me.lstSyntaxKeyList.Columns(2).Text = GetTextInLanguageDoc("StartRegex", TextMode.Text)
        Me.lstSyntaxKeyList.Columns(3).Text = GetTextInLanguageDoc("EndRegex", TextMode.Text)

        MyYesCancelPanel.OK_Text = GetTextInLanguageDoc("OK", TextMode.Text)
        MyYesCancelPanel.NO_Text = GetTextInLanguageDoc("Cancel", TextMode.Text)

        For Each T As SyntaxKey In mConfig.SytaxKeyList
            mSyntaxDic.Add(T.KeyWord, T)
        Next
        RefreshList()
    End Sub
    Private Sub lstSyntaxKeyList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstSyntaxKeyList.DoubleClick
        Me.txtKeyWord.Text = lstSyntaxKeyList.SelectedItems(0).SubItems(0).Text
        Me.NUDLv.Value = lstSyntaxKeyList.SelectedItems(0).SubItems(1).Text
        Me.txtStartSyntax.Text = lstSyntaxKeyList.SelectedItems(0).SubItems(2).Text
        Me.txtEndSyntax.Text = lstSyntaxKeyList.SelectedItems(0).SubItems(3).Text
    End Sub
    Private Sub cmdDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelSyntaxRuler.Click
        If lstSyntaxKeyList.SelectedItems.Count = 0 Then Exit Sub
        mSyntaxDic.Remove(lstSyntaxKeyList.SelectedItems(0).SubItems(0).Text)
        RefreshList()
    End Sub
    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddSyntaxRuler.Click
        If txtKeyWord.Text = "" Then
            MsgBox("请输入阶层标识")
            Exit Sub
        End If
        If txtStartSyntax.Text = "" Then
            MsgBox("请输入开始正则表达式")
            Exit Sub
        End If
        Dim NewKey As New SyntaxKey
        NewKey.KeyWord = Me.txtKeyWord.Text
        NewKey.LV = Me.NUDLv.Value
        NewKey.StartRule = Me.txtStartSyntax.Text
        NewKey.EndRule = Me.txtEndSyntax.Text
        If mSyntaxDic.ContainsKey(NewKey.KeyWord) Then
            mSyntaxDic.Item(NewKey.KeyWord) = NewKey
        Else
            mSyntaxDic.Add(NewKey.KeyWord, NewKey)
        End If
        RefreshList()
    End Sub
    Sub RefreshList()
        lstSyntaxKeyList.Items.Clear()
        For Each T As SyntaxKey In mSyntaxDic.Values
            Me.lstSyntaxKeyList.Items.Add(New ListViewItem(New String() {T.KeyWord, T.LV, T.StartRule, T.EndRule}))
        Next
    End Sub
    Private Sub YesCancelPanel_OK_Clicked() Handles MyYesCancelPanel.OK_Clicked
        mConfig.SytaxKeyList.Clear()
        For Each T As SyntaxKey In mSyntaxDic.Values
            mConfig.SytaxKeyList.Add(T)
        Next
        Me.Close()
    End Sub
    Private Sub YesCancelPanel_No_Clicked() Handles MyYesCancelPanel.NO_Clicked
        Me.Close()
    End Sub
End Class